// import store from "../store";
import MyStore from '@/store/index.js'
// import {
// 	http
// } from '@/utils/http.js'
import config from "@/config"; // 导入配置文件

// 基本路径
const baseURL = config.baseURL;

export function PasswordLoginRequest(password, phoneNumber) {
	console.log("进入密码登录请求");
	console.log(password, phoneNumber);

	// 在发送请求前显示加载框
	uni.showLoading({
		title: '登录中'
	});

	uni.request({
		// 发送到后端url中的login
		url: `${baseURL}/login?username=${phoneNumber}&password=${password}`,
		// url: `/api/login?username=${phoneNumber}&password=${password}`,
		method: "POST",
		success(res) {
			console.log("登陆返回的数据", res);
			if (res.data.code == 200) {
				uni.setStorageSync('token', res.data.message)
				uni.setStorageSync('userData', res.data.data);
			}
			if (res.data.code == 200 && res.data.data.role == 'user') {
				uni.switchTab({
					url: '/pages/index/index'
				});
			} else if (res.data.code == 200 && res.data.data.role == 'employee') {
				uni.redirectTo({
					url: '/pages/maintenance-worker/maintenance-worker'
				})
			} else {
				uni.showToast({
					title: '手机号或密码错误',
					image: '/static/image/icon/fail.png',
					duration: 2000
				});
			}
		},
		fail(err) {
			uni.showToast({
				title: "服务器罢工了",
				image: '/static/image/icon/Disconnected.png',
				duration: 2000
			})
			console.log("服务器罢工了", err);
		},
		complete() {
			// 无论请求成功还是失败，都在这里关闭加载框
			uni.hideLoading();
		}
	});
}

export function VerificationCodeLoginRequest(phoneNumber, verificationCode) {
	// 在发送请求前显示加载框
	uni.showLoading({
		title: '登录中'
	});

	uni.request({
		// 发送到后端url中的login
		url: `${baseURL}/login?username=${phoneNumber}&password=${verificationCode}`,
		method: "POST",
		success(res) {
			console.log("登陆返回的数据", res);
			if (res.data.code === 200) {
				uni.setStorageSync('token', res.data.message)
				uni.setStorageSync('userData', res.data.data);
			}
			if (res.data.code == 200 && res.data.data.role == 'user') {
				console.log("普通用户登陆成功");
				uni.switchTab({
					url: '/pages/index/index'
				});
			} else if (res.data.code == 200 && res.data.data.role == 'employee') {
				console.log("维修员登陆成功");
				uni.redirectTo({
					url: '/pages/maintenance-worker/maintenance-worker'
				})
			} else {
				uni.showToast({
					title: '手机号或验证码错误',
					image: '/static/image/icon/fail.png',
					duration: 2000
				});
			}
		},
		fail(err) {
			console.log("服务器罢工了", err);
			uni.showToast({
				title: "服务器罢工了",
				image: '/static/image/icon/Disconnected.png',
				duration: 2000
			})
		},
		complete() {
			// 无论请求成功还是失败，都在这里关闭加载框
			uni.hideLoading();
		}
	});
}



